package com.baidu.service.impl;
import com.baidu.entity.WycEmail;
import com.baidu.mapper.WycEmailMapper;
import com.baidu.service.EmailService;
import com.baidu.util.EmailUtil;
import com.baidu.utils.ResultResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
 * @Description: 邮箱实现类
 * @Author: 王雨超
 * @Date: 2021/12/13 16:54
 */
@SuppressWarnings({"all"})
@Service
@Slf4j
public class EmailServiceImpl implements EmailService {
    @Autowired
    private WycEmailMapper wycEmailMapper;
    @Override
    public ResultResponse sendEmail(Map map) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        HashMap<String, Object> map1 = new HashMap<>();
        //判断传来的map
        if(map.get("code").equals("1")) {
            //证明是CFO
            WycEmail code = wycEmailMapper.selectById((Serializable) map.get("code"));
            String text=(String) map.get("text"); //邮箱内容
            String to=(String) map.get("to"); //邮箱标签
            /**
             * @param to    收件人邮箱
             * @param text  邮件正文
             * @param title 标题
             * @param USER 邮箱地址
             * @param PASSWORD 授权码
             */
            boolean b = EmailUtil.sendMail(to, text, code.getName(), code.getQq(), code.getCode());
            if(b){
                log.info("{}"+code.getName()+"发送了一份邮件给"+to+"，内容是："+text+"，时间："+sdf.format(new Date()));
                return ResultResponse.SUCCESS();
            }
            return ResultResponse.FAILED(500,"发送失败");
        }else if(map.get("code").equals("2")){
            //证明是CEO
            WycEmail code = wycEmailMapper.selectById((Serializable) map.get("code"));
            String text=(String) map.get("text"); //邮箱内容
            String to=(String) map.get("to"); //邮箱标签
            /**
             * @param to    收件人邮箱
             * @param text  邮件正文
             * @param title 标题
             * @param USER 邮箱地址
             * @param PASSWORD 授权码
             */
            boolean b = EmailUtil.sendMail(to, text, code.getName(), code.getQq(), code.getCode());
            if(b){
                log.info("{}"+code.getName()+"发送了一份邮件给"+to+"，内容是："+text+"，时间："+sdf.format(new Date()));
                return ResultResponse.SUCCESS();
            }
            return ResultResponse.FAILED(500,"发送失败");
        }else{
            return ResultResponse.FAILED(400,"格式不正确");
        }
    }
}
